import { findIndex } from 'lodash'
import { createStore } from 'vuex'
import { setItem, getItem, setItem2 } from '@Config/server/localStorage'
export default createStore({
  state() {
    return {
      singlePath: JSON.parse(getItem('myPath')) || [],
      previewImage: false,
      preivewSrc: null,
      breadcrumb: [],
      folderUi: getItem('preview_type') || 'middle',
      password: null,
      // {
      //   width: 120, //整块区域的宽度
      //   height: 120, //整块区域的高度
      //   selector_size: 30, //选择按钮的font-size
      //   folder_icon: 50, //文件夹icon的font-size
      //   image_width: 120, //图片的最大宽度
      // },
    }
  },
  mutations: {
    spath: (state, payload) => {
      //如果有相同的id，则覆盖他，如果没有的话，直接插入
      let _index = findIndex(state.singlePath, { id: payload.id })
      if (_index >= 0) {
        state.singlePath.splice(_index, 1, { ...payload })
      } else {
        state.singlePath.push({ ...payload })
      }

      setItem('myPath', state.singlePath)
    },
    //重置路径
    rePath: (state, payload) => {
      state.singlePath = payload
    },
    breadcrumbPath: (state, payload) => {
      state.breadcrumb = payload.reverse()
    },
    //全局预览图片
    preivew: (state, payload) => {
      state.previewImage = payload.visible
      state.preivewSrc = payload.src
    },
    //改变文件预览尺寸
    folderUi: (state, payload) => {
      state.folderUi = payload
      setItem2('preview_type', payload)
      // for (var prop in payload) {
      //   state.folderUi[prop] = payload[prop]
      // }
    },
    password: (state, payload) => {
      state.password = payload
      setItem2('password', payload)
    },
  },
  actions: {},
  modules: {},
})
